#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# This is like egrep, but hilites the exact positions
# of *subexpressions*. This is e.g. useful for testing
# inventory_processes...

import re
import sys


def color(x):
    if x == 0:
        return "\033[0m"
    return "\033[1;3%dm" % x


def handle_line(r, line):
    m = r.search(line)
    if m:
        start, end = m.span(0)
        spans = []
        if m.lastindex:
            for i in range(1, m.lastindex + 1):
                spans.append(m.span(i))
        out = ""
        out += line[0:start]
        pos = start
        for b, e in spans:
            out += color(4)
            out += line[pos:b]
            out += color(1)
            out += line[b:e]
            pos = e
        out += color(4)
        out += line[pos:end]
        out += color(0)
        out += line[end:]
        print(out)


rg = re.compile(sys.argv[1])
for line_ in sys.stdin:
    handle_line(rg, line_[:-1])
